#include "calibration.h"
#include "ui_calibration.h"
#include <QPainter>
Calibration::Calibration(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Calibration),
    xOffset(0),
    yOffset(0),
    currentX(0),
    currentY(0),
    animalTimer(NULL)
{
    ui->setupUi(this);
    m_udpsocket = new QUdpSocket();
    if(m_udpsocket->bind(6666,QUdpSocket::ShareAddress))
        qDebug()<<"bing 6666 succeed";
    else
        qDebug()<<"bing failed succeed";

    connect(m_udpsocket,SIGNAL(readyRead()),this,SLOT(receiveData()));

    ui->confirm_Btn->setVisible(false);
    ui->cancel_Btn->setVisible(false);
    ui->calibration_Btn->setVisible(true);

    animalTimer = new QTimer();
    animalTimer->setInterval(80);
    connect(animalTimer,SIGNAL(timeout()),this,SLOT(slotAnimationControl()));

    animalTimer->start();

    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
}

Calibration::~Calibration()
{
    delete ui;
}


void Calibration::receiveData()
{
    QByteArray receiveData;
    //有未处理的数据报
    while(m_udpsocket->hasPendingDatagrams())
    {
        //读取数据报的大小
        receiveData.resize(m_udpsocket->pendingDatagramSize());
        m_udpsocket->readDatagram(receiveData.data(),receiveData.size());

        if(receiveData.size() == 11 && receiveData.at(0) ==0x55 && receiveData.at(1) == 0x53)
        {
            int sum=0;
            for(int i=0;i<10;i++)
            {
                sum+=uint8_t(receiveData.at(i));
            }

            if(uint8_t(receiveData.at(10)) == (sum & 0x000000FF))
            {
                float roll,pitch;

                int high = uint8_t(receiveData.at(3))*255;
                int low = uint8_t(receiveData.at(2));

                roll = (high + low)/100.f;
                pitch = (uint8_t(receiveData.at(5))*255 + uint8_t(receiveData.at(4)))/100.f;

                //前后角度roll
                if(roll >= 180 && roll <= 360)
                    roll = roll-360;

                //左右角度pitch
                if(pitch >= 0 && pitch < 180)
                    pitch *= (-1);
                else if(pitch >= 180 && pitch <= 360)
                    pitch = 360-pitch;

                currentX = pitch;
                currentY = roll;
                ST_RealParam st_realParam;
                st_realParam.xAngle = pitch - xOffset;
                st_realParam.yAngle = roll - yOffset;
                st_realParam.battery = receiveData.at(6);

                //显示左右角度
                angleDisplay(st_realParam.xAngle,st_realParam.yAngle);

                emit sendRealParam(st_realParam);
            }
        }
    }
}


void Calibration::on_calibration_Btn_clicked()
{
    ui->confirm_Btn->setVisible(true);
    ui->cancel_Btn->setVisible(true);
    ui->calibration_Btn->setVisible(false);

    xOffset = currentX;
    yOffset = currentY;

    angleDisplay(0,0);
}


void Calibration::on_cancel_Btn_clicked()
{
    ui->confirm_Btn->setVisible(false);
    ui->cancel_Btn->setVisible(false);
    ui->calibration_Btn->setVisible(true);
}


void Calibration::on_confirm_Btn_clicked()
{
    ui->confirm_Btn->setVisible(false);
    ui->cancel_Btn->setVisible(false);
    ui->calibration_Btn->setVisible(true);

    animalTimer->stop();
    this->close();
}

void Calibration::slotAnimationControl()
{
    static int offset = 0;
    ui->bottom_Label->move(260-offset,44);
    ui->top_Label->move(295-offset,59);
    if(offset >= 80)
        offset = 0;
    offset += 5;
}

void Calibration::angleDisplay(int x, int y)
{
    ui->xAngle_Label->setText(QString::number(x) + "°");
    ui->yAngle_Label->setText(QString::number(y) + "°");
    if(x < 0)
        ui->left_ProgressBar->setValue(-x);
    else if(x > 0)
        ui->right_ProgressBar->setValue(x);
    else
    {
        ui->left_ProgressBar->setValue(0);
        ui->right_ProgressBar->setValue(0);
    }

    if(y < 0)
        ui->down_ProgressBar->setValue(-y);
    else if(x >0)
        ui->up_ProgressBar->setValue(y);
    else
    {
        ui->down_ProgressBar->setValue(0);
        ui->up_ProgressBar->setValue(0);
    }
}

void Calibration::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.fillRect(rect(),QColor(0,0,0,100));
}

void Calibration::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}
